#!/bin/zsh

authUser='root'
anum=$#

getIpPref() {
    ipSuf=$1
    ipSufArr=("${(s/./)ipSuf}")
    len=$#ipSufArr
    if [ $len -eq 1 ] ; then
        ipPref='10.100.1.'
    elif [ $len -eq 2 ] ; then
        ipPref='10.100.'
    elif [ $len -eq 3 ] ; then
        ipPref='10.'
    else
        ipPref=''
    fi
    echo $ipPref
} 

usage() {
    echo "usage: q ip"
    exit 10
}


if [ $anum -ge 1 ] ; then
    ipSuf=$1
    shift
    ipP=$(getIpPref $ipSuf)
else 
    usage
fi

realIp=${ipP}${ipSuf}

if grep "$realIp liaoyongfu" ~/bin/hosts &>/dev/null ; then
    authUser='liaoyongfu'
elif grep "$realIp armin" ~/bin/hosts &>/dev/null ; then 
    authUser='armin'
elif grep "$realIp ec2-user" ~/bin/hosts &>/dev/null ; then
    authUser='ec2-user'
fi

echo ssh -t $authUser@$realIp "$@"
ssh -t $authUser@$realIp "$@"
